// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.popman;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ObjectPool<T> {
    private final ObjectPool.Allocator<T> allocator;
    private final ArrayList<T> pool = new ArrayList<T>() {
        public boolean contains(Object object) {
            for (int _int = 0; _int < ObjectPool.this.pool.size(); _int++) {
                if (ObjectPool.this.pool.get(_int) == object) {
                    return true;
                }
            }

            return false;
        }
    };

    public ObjectPool() {
        this(null);
    }

    public ObjectPool(ObjectPool.Allocator<T> alloc) {
        this.allocator = alloc;
    }

    public T alloc() {
        return (T)(this.pool.isEmpty() ? this.makeObject() : this.pool.remove(this.pool.size() - 1));
    }

    public void release(T object) {
        assert object != null;

        assert !this.pool.contains(object);

        this.pool.add(object);
    }

    public void release(List<T> objs) {
        for (int _int = 0; _int < objs.size(); _int++) {
            if (objs.get(_int) != null) {
                this.release((T)objs.get(_int));
            }
        }
    }

    public void release(Iterable<T> objs) {
        for (Object object : objs) {
            if (object != null) {
                this.release((T)object);
            }
        }
    }

    public void release(T[] object) {
        if (object != null) {
            for (int _int = 0; _int < object.length; _int++) {
                if (object[_int] != null) {
                    this.release((T)object[_int]);
                }
            }
        }
    }

    public void releaseAll(List<T> objs) {
        for (int _int = 0; _int < objs.size(); _int++) {
            if (objs.get(_int) != null) {
                this.release((T)objs.get(_int));
            }
        }
    }

    public void clear() {
        this.pool.clear();
    }

    protected T makeObject() {
        if (this.allocator != null) {
            return this.allocator.allocate();
        } else {
            throw new UnsupportedOperationException(
                "Allocator is null. The ObjectPool is intended to be used with an allocator, or with the function makeObject overridden in a subclass."
            );
        }
    }

    public void forEach(Consumer<T> consumer) {
        for (int _int = 0; _int < this.pool.size(); _int++) {
            consumer.accept(this.pool.get(_int));
        }
    }

    public interface Allocator<T> {
        T allocate();
    }
}
